// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成

package com.fowo.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fowo.api.model.supplier.flow.form.relation.SupplierFlowFormRelationSearchParamPo;
import com.fowo.api.model.supplier.report.*;
import com.fowo.api.sys.model.SysDictAllItem;
import com.fowo.api.sys.service.SysDictService;
import com.fowo.api.user.model.JwtUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 供应商报表 服务实现类
 *
 * @author 蜂窝蜜造平台 配置人：任凯
 */
@Service
@Slf4j
@DS("ds24")
public class SupplierReportServiceImpl extends SupplierReportBaseServiceImpl {
    @Resource
    private SupplierFlowFormRelationServiceImpl supplierFlowFormRelationService;

    /**
     * 任务分析
     *
     * @return
     */
    public Map selectNavTask(SupplierFlowFormRelationSearchParamPo search) {
        final String SCM00195 = "SCM00195";
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
            search.setCurrentUserId(jwtUserInfo.getUserId());
        }
        List<SysDictAllItem> itemList = SpringUtil.getBean(SysDictService.class).getDictItems(SCM00195);
        MapBuilder<Object, Object> builder = MapUtil.builder();
        for (SysDictAllItem item : itemList) {
            search.setEnCode(item.getItemValue());
            builder.put(item.getItemText(), CollUtil.newArrayList(supplierFlowFormRelationService.getBaseMapper().allCount(search), supplierFlowFormRelationService.getBaseMapper().processingCount(search)));
        }
        return builder.build();
    }

    /**
     * 及时率&综合评分
     */
    public SupplierReportSelectResult supplierReport(
            SupplierReportSelectParam param
    ) {
        //默认本年
        if (StrUtil.isBlank(param.getCreateTimeStart())) {
            param.setCreateTimeStart(DateUtil.formatDateTime(DateUtil.beginOfYear(DateUtil.date())));
        }
        if (StrUtil.isBlank(param.getCreateTimeEnd())) {
            param.setCreateTimeEnd(DateUtil.formatDateTime(DateUtil.endOfYear(DateUtil.date())));
        }
        SupplierReportSelectResult result = new SupplierReportSelectResult();
        result.setSpeedRateAndScoreResult(this.selectSpeedRateAndScore(BeanUtil.copyProperties(param, SupplierReportSelectSpeedRateAndScoreParam.class))); //及时率&综合评分
        result.setPassCountResult(this.selectPassCount(BeanUtil.copyProperties(param, SupplierReportSelectPassCountParam.class)));  //合格率
        result.setTransformCountResult(this.selectTransformCount(BeanUtil.copyProperties(param, SupplierReportSelectTransformCountParam.class)));//转化率
        SupplierReportSelectPurchaseOrderCountParam _param = BeanUtil.copyProperties(param, SupplierReportSelectPurchaseOrderCountParam.class);
        result.setPurchaseOrderCountResult(this.selectPurchaseOrderCount(_param));//历史采购
        _param.setCreateTimeStart(DateUtil.formatDateTime(DateUtil.beginOfDay(DateUtil.date())));
        _param.setCreateTimeEnd(DateUtil.formatDateTime(DateUtil.endOfDay(DateUtil.date())));
        result.setPurchaseOrderCountResultToday(this.selectPurchaseOrderCount(_param));//今日采购
        _param.setCreateTimeStart(DateUtil.formatDateTime(DateUtil.beginOfMonth(DateUtil.date())));
        _param.setCreateTimeEnd(DateUtil.formatDateTime(DateUtil.endOfMonth(DateUtil.date())));
        result.setPurchaseOrderCountResultMonth(this.selectPurchaseOrderCount(_param));//本月采购
        result.setTaskInstanceCountResult(this.selectTaskInstanceCount(BeanUtil.copyProperties(param, SupplierReportSelectTaskInstanceCountParam.class)));//开发中
        result.setSupplyCountResult(this.selectSupplyCount(BeanUtil.copyProperties(param, SupplierReportSelectSupplyCountParam.class)));//供货中
        result.setPatrolCountResult(this.selectPatrolCount(BeanUtil.copyProperties(param, SupplierReportSelectPatrolCountParam.class)));//巡场中
        result.setReviewCountResult(this.selectReviewCount(BeanUtil.copyProperties(param, SupplierReportSelectReviewCountParam.class)));//评审中

        result.setTaskCountResult(this.selectNavTask(new SupplierFlowFormRelationSearchParamPo()));

        result.setGoodResult(this.selectGood());//本年度供货订单合格率分析
        result.setReceivedResult(this.selectReceived());//本年度供货订单及时率分析
        result.setWeightCountResult(this.selectWeightCount());//本年度供货订单转化率分析
        return result;
    }
}
